package me.shurufa.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.shurufa.R;
import me.shurufa.activities.UserCommentActivity;
import me.shurufa.model.Comment;
import me.shurufa.model.CommentListResponse;
import me.shurufa.model.User;
import me.shurufa.net.NetworkConstants;
import me.shurufa.net.RequestServerTask;
import me.shurufa.utils.Constants;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.Utils;
import me.shurufa.widget.EllipsizingTextView;

/**
 * 点击用户头像 多布局ScrollView中的fragment 最多只显示三条
 *
 * @FileName: me.shurufa.fragments.UserInfoCommentFragment.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-27 22:38
 */
public class UserInfoCommentFragment extends BaseFragment implements View.OnClickListener {

    @Bind(R.id.divider)
    View divider;
    @Bind(R.id.watch_all)
    TextView watchAll;
    @Bind(R.id.comment_container)
    LinearLayout commentContainer;
    @Bind(R.id.comment_root)
    LinearLayout commentRoot;
    @Bind(R.id.empty_view)
    RelativeLayout emptyView;
    private long userId;
    private User user;
    private LayoutInflater inflater;

    public static UserInfoCommentFragment newInstance(User user) {
        UserInfoCommentFragment fragment = new UserInfoCommentFragment();
        Bundle args = new Bundle();
        args.putLong(Constants.ARG_USER_ID, user.uid);
        fragment.user = user;
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            userId = getArguments().getLong(Constants.ARG_USER_ID);
        }
        inflater = LayoutInflater.from(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_comment, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        showEmptyView();
        watchAll.setOnClickListener(this);
        initData();

    }

    private void initData() {
        new RequestServerTask<CommentListResponse>(CommentListResponse.class){

            @Override
            protected String requestServer() {
                Map<String,Object> postParams = new HashMap<String, Object>();
                postParams.put("userId",userId);
                postParams.put("pageSize",3);
                return HttpUtil.post(NetworkConstants.COMMENT_GET_BY_USER, postParams);
            }

            @Override
            protected void onSuccess(CommentListResponse result) {
                if (result.data!=null&&result.data.length>0){
                    initCommentLayout(result.data);
                }else {
                    //TODO 显示还没有评价
                }
            }
        }.start();
    }

    /**
     * 动态添加布局至container
     */
    private void initCommentLayout(Comment[] comments) {
        if (comments.length>0) {
            hideEmptyView();
            for (int i = 0; i < comments.length; i++) {
                Comment comment = comments[i];
                if (comment != null) {
                    View view = inflater.inflate(R.layout.list_item_user_info_comment, commentContainer, false);
                    ImageView imageView = (ImageView) view.findViewById(R.id.book_img);
                    if (comment.book_info != null && !TextUtils.isEmpty(comment.book_info.image)) {
                        Glide.with(getActivity()).load(comment.book_info.image).into(imageView);
                    } else {
                        //TODO 设置默认图片
                    }
                    EllipsizingTextView commentText = (EllipsizingTextView) view.findViewById(R.id.book_comment);
                    commentText.setMaxLines(6);
                    if (!TextUtils.isEmpty(comment.content))
                        commentText.setText(comment.content);
                    commentContainer.addView(view);
                }
            }
        }else {
            showEmptyView();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.watch_all:
                if (user!=null){
                    Intent intent = new Intent(getActivity(), UserCommentActivity.class);
                    intent.putExtra(Constants.EXTRA_USER_PARAM,user.toString());
                    startActivity(intent);
                }
                break;
        }
    }

    public void showEmptyView(){
        emptyView.setVisibility(View.VISIBLE);
    }

    public void hideEmptyView(){
        emptyView.setVisibility(View.GONE);
    }
}
